<?php

class WSB_MimeTypes
{

	/**
	 * @var	array
	 */
	private static  $mime_types = array();

	/**
	 * @var boolean
	 */
	private static $mime_types_loaded = false;

	/**
	 * @param string $file_path_abs
	 * @return string
	 */
	public static function getMimeType($file_path_abs = '')
	{
		if(empty($file_path_abs)) {
			return '';
		}
		$extension = substr(strtolower(strrchr($file_path_abs, '.')),1);
		if (!self::$mime_types_loaded) {
			self::loadMimeTypes();
		}
		if (array_key_exists($extension, self::$mime_types)) {
			return self::$mime_types[$extension];
		}
		else {
			return 'text/plain';
		}
	}

	/**
	 * @return void
	 */
	private static function loadMimeTypes()
	{
		$file = dirname(__FILE__) . '/mime_types.txt';
		if (file_exists($file)) {
			foreach (file($file) as $line) {
				list($mime_type, $extensions) = split("\t", rtrim($line));
				foreach (split(' ', $extensions) as $extension) {
					self::$mime_types[$extension] = $mime_type;
				}
			}
		}
		self::$mime_types_loaded = true;
	}
}
